﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#pragma once
#include "ActionBase.h"
#include "Vector3d.h"
#include "ElementCreationOptions.h"
#include "GridGroupNewParam.h"

namespace gcmp
{
    class IGridNew;
    class ICurve3d;

    //批量轴网基类
    class ActionCreateGridGroupNewBase : public gcmp::ActionBase
    {
        CANNOT_COPY_OR_ASSIGN(ActionCreateGridGroupNewBase)
    private:
        enum EnDrawGridGroupStatus
        {
            IDS_Not_Started = 0,
            IDS_Getting_PlacePoint = 1,
            IDS_Getting_DirectionPoint = 2,
        };
    public:
        ActionCreateGridGroupNewBase();

    //继承自ActionBase:
    public:
        virtual void InitAction(gcmp::IUiView* pCurrentView) override;
        virtual void OnChildActionFinished(gcmp::IUiView* pCurrentView, const gcmp::ActionOutput& childActionReturnParam) override;

    //子类需实现的虚函数：
    protected:
        virtual  std::vector<IGridNew*> CreateGridGroup(
            IDocument* pDoc,
            const IModelView* pModelView,
            const Vector3d& anchorPt,
            double rotateAngle = 0,
            ElementCreationOptions eco = ElementCreationOptions::Normal) = 0;

    private:
        void GettingGridGroupPlacePoint();
        void GettingGridGroupDirectionPoint();
        void PreviewByPlacePoint(gcmp::IUiView* pCurrentView, const gcmp::Vector3d& pos, const gcmp::Vector3d& mousePt);
        void PreviewByPlacePointWithDirection(gcmp::IUiView* pCurrentView, const gcmp::Vector3d& pos, const gcmp::Vector3d& mousePt);

        double GetRotateAngleOnWorkPlane(const Vector3d &start, const Vector3d &end, const IModelView* pModelView) const;
        std::wstring GetPromptMessage() const override;
        OwnerPtr<IGraphicsNodeGroup> GetGridGroupGrep(const std::vector<IGridNew*> &gridGroup, const IModelView* pModelView) const;
        
        //完全删除时返回true,否则false
        bool DeleteGridGroup(IDocument *pDoc, std::vector<IGridNew*> vecGrids);

    protected:
        IGridNew* CreateGridByCurveAndSetProperty(
            IDocument* pDoc,
            const IModelView* pModelView,
            const ICurve3d* pCurve, 
            const std::wstring& name,
            ElementCreationOptions eco = ElementCreationOptions::Normal,
            bool startTagVisible=true,
            bool endTagVisible=true
        );

        void SetRotatable(bool bRotate) { m_bRotate = bRotate; }

    private:
        //用户是否取消action
        bool m_isUserCancelled;
        //绘制批量轴网的状态：未开始、正在取锚点、正在旋转
        EnDrawGridGroupStatus m_drawGridGroupStatus;
        //锚点位置（第1点）
        Vector3d m_placePt;
        //旋转轴网时方向点（第2点）位置
        Vector3d m_directionPt;
        //是否旋转绘制轴网
        bool m_bRotate;
        std::vector<Vector3d> m_pickedPoints;
    };

    //批量正交轴网
    class ActionCreateOrthogonalGridGroupNew :public ActionCreateGridGroupNewBase
    {
    public:
        ActionCreateOrthogonalGridGroupNew(const OrthogonalGridGroupParam &param);

    private:
        virtual  std::vector<IGridNew*> CreateGridGroup(
            IDocument* pDoc,
            const IModelView* pModelView,
            const Vector3d& anchorPt,
            double rotateAngle = 0,
            ElementCreationOptions eco = ElementCreationOptions::Normal) override;

    private:
        OrthogonalGridGroupParam m_param;
    };

    //批量辐射轴网
    class ActionCreateRadialGridGroupNew :public ActionCreateGridGroupNewBase
    {
    public:
        ActionCreateRadialGridGroupNew(const RadialGridGroupParam& param);

    private:
        virtual  std::vector<IGridNew*> CreateGridGroup(
            IDocument* pDoc,
            const IModelView* pModelView,
            const Vector3d& anchorPt,
            double rotateAngle = 0,
            ElementCreationOptions eco = ElementCreationOptions::Normal) override;

    private:
        RadialGridGroupParam m_param;
    };

};